package com.chinaums.xm.wechat.service.wechat;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chinaums.xm.wechat.WeixinConstant;
import com.chinaums.xm.wechat.domain.wechat.AccessToken;
import com.chinaums.xm.wechat.domain.wechat.mass.Filter;
import com.chinaums.xm.wechat.domain.wechat.mass.Image;
import com.chinaums.xm.wechat.domain.wechat.mass.ImageMassMessage;
import com.chinaums.xm.wechat.domain.wechat.mass.Text;
import com.chinaums.xm.wechat.domain.wechat.mass.TextMassMessage;
import com.chinaums.xm.wechat.service.Communicator;

import net.sf.json.JSONObject;

/**
 * 群发service
 * @author chenjf
 *
 */
@Service
public class MassService {
	
	@Resource
	private CommonService commonService;
	
	@Resource
	private Communicator communicator;
	
	/**
	 * 群发文本消息
	 */
	public boolean sendNewsMsgAll(){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String token = accessToken.get().getToken();
			final String url = WeixinConstant.MESSAGE_SENDALL_URL.replace("ACCESS_TOKEN", token);
			final ImageMassMessage imageMassMessage = new ImageMassMessage();
			final Image image = new Image();
			image.setMedia_id("A5MxcHWx2NC8R9iayFlT5entjT39VefdSTqrgITtn8QzQU0NYjDa6kbb7NwqHmtViaHV4URMgY29ICkicURDUGpFQ");
			final Filter filter = new Filter();
			filter.setIs_to_all(true);
			imageMassMessage.setFilter(filter);
			imageMassMessage.setImage(image);
			imageMassMessage.setMsgtype("image");
			final JSONObject fromObject = JSONObject.fromObject(imageMassMessage);
			final Optional<String> responseMsg = communicator.postRequest(url, String.valueOf(fromObject));
			if(responseMsg.isPresent()){
				final JSONObject response = JSONObject.fromObject(responseMsg.get());
				final String errCode = response.getString("errcode");
				if(errCode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
	
	/**
	 * 群发图文消息
	 */
	public boolean sendTextMsgAll(final String content){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String token = accessToken.get().getToken();
			final String url = WeixinConstant.MESSAGE_SENDALL_URL.replace("ACCESS_TOKEN", token);
			final TextMassMessage textMassMessage = new TextMassMessage();
			final Text text = new Text();
			text.setContent(content);
			final Filter filter = new Filter();
			filter.setIs_to_all(true);
			textMassMessage.setFilter(filter);
			textMassMessage.setText(text);
			textMassMessage.setMsgtype("text");
			final JSONObject fromObject = JSONObject.fromObject(textMassMessage);
			final Optional<String> responseMsg = communicator.postRequest(url, String.valueOf(fromObject));
			if(responseMsg.isPresent()){
				final JSONObject response = JSONObject.fromObject(responseMsg.get());
				final String errCode = response.getString("errcode");
				if(errCode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
}
